package com.su.sweet.controller;

import com.su.sweet.annotation.Wrapper;
import com.su.sweet.model.SUser;
import com.su.sweet.service.ISUserService;
import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jwts;
import org.noear.solon.annotation.*;
import org.noear.solon.core.handle.Context;
import org.noear.solon.core.handle.Result;
import org.noear.solon.sessionstate.jwt.JwtUtils;
import cn.hutool.core.util.RandomUtil;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author su
 * @since 2025-01-02
 */
@Controller
@Mapping("/user")
public class SUserController {
    @Inject
    private ISUserService userService;

    @Mapping("/list")
    public Result geUser(SUser user, Integer current, Integer pageSize) {
        return Result.succeed(userService.selectUserList(user, current, pageSize));
    }

    @Mapping("/getALlUser")
    @Wrapper(UserWapper.class)
    public Result getALlUser() {
        return Result.succeed(userService.getALlUser());
    }

    @Mapping("/login")
    public Result login(SUser user, Context ctx) {
        SUser res = userService.login(user);
        if (res != null) {
            Claims claims = Jwts.claims();
            claims.put("user", res);
            List<String> list = new ArrayList<>();
            list.add("ADMIN");
            res.setRoles(list);
            res.setToken(JwtUtils.buildJwt(claims, 1000 * 60 * 20));
            res.setLastLoginIp(ctx.realIp());
            res.setLastLoginTime(LocalDateTime.now());
            res.setLoginCount(res.getLoginCount() + 1);
            userService.updateUser(user);
        }
        //sessionToken() 接口，目前仅在 solon.sessionstate.jwt 插件中支持
        return Result.succeed(res);
    }

    @Get
    @Mapping("/info")
    public Result info(Context ctx) {
        String token = ctx.header("authorization");
        return Result.succeed(JwtUtils.parseJwt(token).get("user"));
    }

    @Get
    @Mapping("/codes")
    public Result codes(Context ctx) {
        List<String> codes = new ArrayList<>();
        codes.add("AC_100100");
        codes.add("AC_100110");
        codes.add("AC_100120");
        codes.add("AC_100010");
        return Result.succeed(codes);
    }

    @Get
    @Mapping("/logout")
    public Result logout(Context ctx) {
        return Result.succeed(null);
    }

    @Get
    @Mapping("/refreshToken")
    public Result refreshToken(Context ctx) {
        String token = ctx.header("authorization");
        SUser user = (SUser) JwtUtils.parseJwt(token).get("user");
        user.setPasswordSalt(RandomUtil.randomString(10));
        Claims claims = Jwts.claims();
        claims.put("user", user);
        return Result.succeed(JwtUtils.buildJwt(claims, 1000 * 60 * 20));
    }

    @Post
    @Mapping("/save")
    public Result save(Context ctx, SUser user) {
        user.setCreateUser(Long.valueOf(ctx.header("userId")));
        user.setCreateTime(LocalDateTime.now());
        user.setSuperAdminFlag("N");
        user.setStatusFlag(1);
        user.setLoginCount(0);
        user.setLastLoginIp(ctx.realIp());
        user.setPasswordSalt(RandomUtil.randomString(10));
        return Result.succeed(userService.insertUser(user));
    }

    @Post
    @Mapping("/update")
    public Result update(Context ctx, SUser user) {
        user.setUpdateUser(Long.valueOf(ctx.header("userId")));
        user.setUpdateTime(LocalDateTime.now());
        return Result.succeed(userService.updateUser(user));
    }

    @Post
    @Mapping("/delete")
    public Result delete(Integer id) {
        return Result.succeed(userService.deleteUser(id));
    }
}
